using Godot;
using System;

public partial class player : Node2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        Name = GetMultiplayerAuthority().ToString();

    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }

    public override void _PhysicsProcess(double delta)
    {
        if (IsMultiplayerAuthority())
        {
            if (Input.IsActionJustPressed("ui_left"))
            {
                Position = Position + Vector2.Left * 10;
                Rpc(nameof(RemoteSetPosition), Position);
            }
            else if (Input.IsActionJustPressed("ui_up"))
            {
                Position = Position + Vector2.Up * 10;
                Rpc(nameof(RemoteSetPosition), Position);
            }
            else if (Input.IsActionJustPressed("ui_right"))
            {
                Position = Position + Vector2.Right * 10;
                Rpc(nameof(RemoteSetPosition), Position);
            }
            else if (Input.IsActionJustPressed("ui_down"))
            {
                Position = Position + Vector2.Down * 10;
                Rpc(nameof(RemoteSetPosition), Position);
            }

        }
        base._PhysicsProcess(delta);


    }

    [Rpc(TransferMode = MultiplayerPeer.TransferModeEnum.Unreliable)]
    public void RemoteSetPosition(Vector2 authorityPosition)
    {
        Position = authorityPosition;
    }
}
